$(document).ready(function() {	
	$('textarea#fckcontent').ckeditor();	
	$('#frmEdit').validate({
		errorClass		: 'error',
		errorElement	: 'span',
		onfocusout		: false,
		onkeyup			: false,
		ignore			: 'input[type="text"]:hidden',
		rules: {
			pcategoryid: {
				min						: 1
			},
			pname:{
				required				: true,
				minlength				: VL_NAME_MINLENGTH
			},		
			plongitude:{				
				number					: true
			},
			platitude:{				
				number					: true
			},
			pprovince:{
				min						: 1
			},
			pdistrict:{
				min						: 1
			},
			paddr: {
				required				: true
			},
			pimage: {				
				validateImgFormat		: true,
				validateImgSize			: true
			},				
			pcontent: {
				/*validateContentRequired	: true,*/
				validateContentMinlength: true
			},
			plink: {
				url						: true
			},
			ptbfeature: {
				validateFeature			: true
			}
		},
		messages: {		
			pcategoryid: {
				min						: categoryid_required
			},		
			pname:{				
				minlength				: name_minlength
			},	
			plongitude:{				
				number					: longitude_number
			},
			platitude:{
				required				: latitude_required,
				number					: latitude_number
			},
			pprovince:{
				min						: province_required
			},
			pdistrict:{
				min						: district_required
			},	
			paddr: {
				required				: addr_required
			},
			pimage: {
				validateImgFormat		: logo_format,
				validateImgSize			: logo_size
			},			
			pcontent: {
				/*validateContentRequired	: content_required,*/
				validateContentMinlength: content_minlength
			},
			plink: {
				url						: link_format
			},
			ptbfeature : {
				validateFeature			: feature_valid
			}
		},
		errorPlacement: function(label, element) {
			var id = element.attr('id');	
			if(id == 'pcontent') {			
				label.insertAfter($('#cke_fckcontent'));
			}else if(id == 'ptbfeature'){
				label.insertAfter($('#featureTbl'));	
			}  else {
				label.insertAfter($('#' + id));			
			}
			
		},
		submitHandler: function(form) {	
			var tblen	  = $('#featureTbl tbody tr').length;
			if(tblen > 0) {
				var tbfeature = [];
				for(index=1;index<=tblen;index++) {
					var id	= $('#featureTbl tbody tr:nth-child('+index+')').attr('data-id');					
					var lbl = $('#td_lbl_' + id + ' input').val();
					var tag = $('#td_tag_' + id + ' select').val();
					if(tag == 'checkbox') {
						var val    = [];
						var divlen = $('#featureTbl tbody #div_val_' + id + ' p').length;
						if(divlen > 0) {							
							for(i=0;i<divlen;i++) {
								var sid	= $('#featureTbl tbody #div_val_' + id + ' p').attr('data-id');
								var chk	= $('#chk_' + id + '_' + sid).attr('checked');								
								var inp	= $('#inp_' + id + '_' + sid).val();
								val.push({"chk":(chk == 'checked' ? 1 : 0),"inp":inp});
							}							
						}						
					} else {
						var val = $('#tbVal' + id).val();														
					}
					tbfeature.push({"lbl":lbl,"tag":tag,"val":val});
				}
				$('#ptbfeature').val(JSON.stringify(tbfeature));				
			}
			var full_addr = $('#paddr').val();
			full_addr 	 += ', ' + $('#pdistrict option:selected').text();
			full_addr 	 += ', ' + $('#pprovince option:selected').text();
			$('#pfulladdr').val(full_addr);
			$('#pcontent').val($('#fckcontent').val());
			form.submit();
		}				 
	});
	$.validator.addMethod('validateImgFormat', function() {
		var strLinkImage = $('#pimage').val();
		if(strLinkImage == '') {
			return true;
		}
		return validateImage(strLinkImage);
	});
	$.validator.addMethod('validateImgSize', function() {
		var strLinkImage = $('#pimage').val();
		if(strLinkImage == "") {
			return true;
		}
		return validateImgSize('pimage');
	});	
	
	$.validator.addMethod('validateContentRequired',function(){
		var strContent		= stripTags($('#fckcontent').val());		
		if(strContent == '')
			return false;		
		return true;												 
	});
	$.validator.addMethod('validateContentMinlength',function(){
		var strContent		= stripTags($('#fckcontent').val());		
		if(strContent != '' && strContent.length < VL_CONTENT_MINLENGTH)
			return false;		
		return true;												 
	});	
	$.validator.addMethod('validateFeature', function() {
		var trLen = $('#featureTbl tbody tr').length;		
		var flag  = true;
		if(trLen == 1) {
			var trId = $('#featureTbl tbody tr').attr('data-id');
			var lbl  = $('#tbLabel' + trId).val();
			var tag  = $('#tbTag' + trId).val();
			if(tag == 'checkbox') {
				$('#td_val_' + trId + ' input[type="text"]').each(function(){
					if($(this).val() == '') {
						flag = false;						
					}
					if(flag == false) return false;
				});
			} else {
				var val  = $('#tbVal' + trId).val();
				if(val != '' && lbl == '') {
					flag = false;
				}
			}			
		} else {
			for(i=0;i<trLen;i++) {
				var id	= $('#featureTbl tbody tr:nth-child('+i+')').attr('data-id');		
				var lbl = $('#tbLabel' + id).val();
				var tag = $('#tbTag' + id).val();				
				if(tag == 'checkbox') {
					$('#td_val_' + id + ' input[type="text"]').each(function(){
						if($(this).val() == '') {
							flag = false;						
						}
						if(flag == false) return false;
					});
				} else {
					var val	= $('#tbVal' + id).val();
					if(val == '' || lbl == '') {
						flag = false;
						break;
					}
				}				
			}
		}
		return flag;
	});
});